Ext.define('Projeto.controller.Observatory', {
	extend: 'Ext.app.Controller',
	
	stores: ['Observatories'],
	
	init: function() {
		this.control({
			'observatorylist': {
				render: this.onListRender,
				itemdblclick: this.onItemClick
			},
			
			'observatorylist > toolbar > button[action=new]' : {
                click: this.newObsevatory
            },
			
			'observatoryedit > form > toolbar > button[action=save]': {
            	click: this.onSaveObs
            }
		});
		
	},
	
	
	onListRender: function() {
		this.getObservatoriesStore().load();
	},
	
	newObsevatory: function() {
		var view = Ext.widget('observatoryedit');
	},
	
	onItemClick: function(view, record, item, index, event, opts) {
    	var view = Ext.widget('observatoryedit');
    	var form = view.down('form');
    	form.loadRecord(record);
    },
	
	onSaveObs: function(button) {
    	var form = button.up('form');
    	var record = form.getRecord();
    	var values = form.getValues();
    	
    	if (!record) {
    		var obs = Ext.create('Projeto.model.Observatory');
    		obs.set(values);
    		this.getObservatoriesStore().add(obs);
    	} else {
    		record.set(values);
    	}
    	
    	this.getObservatoriesStore().sync();
    	this.getObservatoriesStore().load();
    	form.up('window').close();
    }
});